﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CH.Study.ConsoleApp.Linq
{

    public class LinqTest : ITest
    {

        protected int GetPercentile(Student s)
        {
            double avg = s.ExamScores.Average();
            return avg > 0 ? (int)avg / 10 : 0;
        }
        public void QueryHighScores(int exam, int score)
        {
            var highScores = from student in TestData.students
                             where student.ExamScores[exam] > score
                             select new { Name = student.FirstName, Score = student.ExamScores[exam] };

            foreach (var item in highScores)
            {
                Console.WriteLine("{0,-15}{1}", item.Name, item.Score);
            }
        }
        public void GroupBySingleProperty()
        {
            Console.WriteLine("Group by a single property in an object:");

            // Variable queryLastNames is an IEnumerable<IGrouping<string, 
            // DataClass.Student>>. 
            var queryLastNames =
                from student in TestData.students
                group student by student.LastName into newGroup
                orderby newGroup.Key
                select newGroup;

            foreach (var nameGroup in queryLastNames)
            {
                Console.WriteLine("Key: {0}", nameGroup.Key);
                foreach (var student in nameGroup)
                {
                    Console.WriteLine("\t{0}, {1}", student.LastName, student.FirstName);
                }
            }
        }



        public void Start()
        {
            // QueryHighScores(1, 6);
            //GroupBySingleProperty();
            QHomeEntities ctx = new QHomeEntities();
            ctx.AddToMyHome(new MyHome());
        }
    }
}
